package com.putao.album.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.util.ViewHelper;

/**
 * Created by jidongdong on 15/5/7.
 */
public class MessageDialogBuilder extends DialogBuilder {
    private int type;
    private String mMessage;
    private Context context;

    public MessageDialogBuilder(Context context) {
        super(context);
        this.context = context;
    }

    public MessageDialogBuilder setMessage(String message) {
        this.mMessage = message;
        return this;
    }

    public MessageDialogBuilder setMessageType(int type) {
        this.type = type;
        return this;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_pt_message_layout;
    }

    @Override
    protected void doInitSubView(View view) {
        Button btn_confirm = ViewHelper.queryViewById(view, R.id.btn_confirm);
        if (btn_confirm != null) {
            btn_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDialog().dismiss();
                }
            });
        }
        ImageView tips_image = ViewHelper.queryViewById(view, R.id.tips_image);
        if (tips_image != null) {
            if (this.type == 2) {
                tips_image.setVisibility(View.GONE);
            }
        }
        TextView textView = ViewHelper.queryViewById(view, R.id.tv_message);
        TextView tv_sub_title = ViewHelper.queryViewById(view, R.id.tv_sub_title);
        if (!TextUtils.isEmpty(mMessage) && textView != null) {
            String[] strings;
            if (this.type == 1) {
                strings = mMessage.split("_");
                String format = context.getString(R.string.dialog_message_text);
                if (!TextUtils.isEmpty(format) && strings.length > 1) {
                    textView.setText(Html.fromHtml(String.format(format, strings[0], strings[1])));
                }
            } else if (type == 2) {
                strings = mMessage.split("，");
                if (strings.length > 1) {
                    textView.setText(strings[0]);
                    if (tv_sub_title != null) {
                        tv_sub_title.setText(strings[1]);
                    }
                }
            }

        }
    }

    @Override
    protected void setWindowAnim(Dialog dialog) {
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.dialog_message_style);
    }

    @Override
    protected void doSetupData() {

    }
}
